package com.example.orderhandle;

import com.example.orderhandle.entity.Order;
import com.example.orderhandle.service.OrderServiceImp;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import java.util.Map;

@Component
@Controller
@RabbitListener(queues = "TestDirectQueue")//监听的队列名称 TestDirectQueue
public class DirectReceiver {
    //根据收到的信息对数据库进行插入操作
    @Autowired
    OrderServiceImp orderServiceImp;

    @RabbitHandler
    public void process(Map testMessage) {
        //取出testMessage中的信息
        System.out.println("DirectReceiver消费者收到消息  : " + testMessage.toString());
        String buildingName = (String) testMessage.get("buildingName");
        String gender = (String) testMessage.get("gender");
        int num = (int) testMessage.get("num");
        int uid = (int) testMessage.get("uid");
        String submitTime = (String) testMessage.get("submitTime");
        int userUids = (int)testMessage.get("userUids");
        int roomId = (int)testMessage.get("roomId");
        //生成order信息
        Order order = new Order();
        int buildingId = orderServiceImp.findBuildingIdByBuildingName(buildingName); //这一行在消息队列中处理
        order.setGender(gender);
        order.setDormBuildingId(buildingId);//这一行在消息队列中处理
        order.setIsSuccess("1");
        order.setStuNum(num);
        order.setUid(uid); //提交人的uid
        order.setSubmitTime(submitTime);
        System.out.println("开始进行数据库操作");
        //进行数据库操作
        orderServiceImp.grabDorm(roomId,num,order,userUids);
        System.out.println("操作成功");
    }

}